package cn.com.wxd.action.admin.website;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.wxd.action.AjaxResult;
import cn.com.wxd.biz.log.AccessLogService;
import cn.com.wxd.biz.user.UsersService;
import cn.com.wxd.biz.website.ArticleService;
import cn.com.wxd.entity.user.AccountInfo;
import cn.com.wxd.entity.website.WebInfo;
import cn.com.wxd.util.action.AccessLogUtil;
import cn.com.wxd.util.framework.LocalCacheUtil;

/**
 * Title:
 * Description:
 * <p>
 * Company: Amumu管理平台
 * Copyright: Copyright (c) 2017
 * All right reserved.
 * Created by WangXuDong on 2017/6/4.
 *
 * @author WangXuDong
 * @version 1.0
 */
@Controller
@RequestMapping("/admin/website")
public class WebsiteInfoCtl {

    private static final Logger log = LoggerFactory.getLogger(WebsiteInfoCtl.class);

    @Resource
    private UsersService usersService;

    @Resource
    private ArticleService articleService; //服务层

    @Resource
    private AccessLogService accessLogService;//服务层

    /**
     * 获取网站信息页面
     *
     * @param map
     *
     * @return
     */
    @RequestMapping("/webinfo")
    private String webinfo(ModelMap map) {
        LocalCacheUtil.getInstance().getCache("webinfocache");
        WebInfo webInfo = new WebInfo();
        webInfo.setTotalArticleNum(articleService.getArtirlCount());
        webInfo.setOnlineUserCount(AccessLogUtil.getOnlineUser());
        webInfo.setTotalVisiVol(AccessLogUtil.getTatorAccessVolume());
        webInfo.setTotaluserNum(usersService.getUserCount());
        webInfo.setTodayVisiVol(AccessLogUtil.getToDayAccessVolume());
        map.put("webInfo", webInfo);
        return "/admin/website/web_info";
    }

    /**
     * 获取一周访问量
     *
     * @return
     */
    @RequestMapping("/weekVisitorVolume")
    @ResponseBody
    public AjaxResult weekVisitorVolume() {
        String reStr = "";
        int[] ints = accessLogService.queryGroupByDay(7);
        if (ints != null && ints.length > 0) {
            for (int j = 0; j < ints.length; j++) {
                reStr += ints[j] + ",";
            }
            reStr = reStr.substring(0, reStr.length() - 1);
        } else {
            reStr += "0,0,0,0,0,0,0";
        }
        log.debug(reStr);
        return AjaxResult.successResult(reStr);
    }

    /**
     * 获取用户列表Dom
     *
     * @return
     */
    @RequestMapping(value = "/userListDom")
    @ResponseBody
    public AjaxResult getUserlistDom(int count) {
        String reStr = "";
        if (count <= 0) {
            count = 4;
        }
        List<AccountInfo> list = usersService.queryAllAccountListOrderCTime(count);
        if (list == null) {
            list = new ArrayList<>();
        }
        for (int i = 0; i < list.size(); i++) {
            reStr += "<tr><td>" + list.get(i).getUsersInfo().getNickName() + "</td>";
            if (!list.get(i).getType().equalsIgnoreCase("QQ")) {
                reStr += "<td>" + list.get(i).getUserName() + "</td>";
            } else {
                reStr += "<td>腾讯QQ用户</td>";
            }
            reStr += "<td>" + list.get(i).getLalogTime() + "</td></tr>";
        }
        return AjaxResult.successResult(reStr);
    }
}
